<?php
/**
 * Created by PhpStorm.
 * User: XZR
 * Date: 2018/8/6
 * Time: 15:14
 */

namespace Admin\Model;



use Think\Model;

class CommentModel extends Model
{
    protected $tablePrefix = "";
    protected $tableName = 'tb_md_comment';


    /* 自动验证规则 */
    protected $_validate = array(

        array('name', 'require', '评论人必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('hospital','require','医院必须',self::MUST_VALIDATE,'regex',self::MODEL_BOTH),
        array('comment', 'require', '评论必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('comment_date', 'require', '评论时间必须', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    );
    private static $instance;

    public static function singleton()
    {
        self::$instance === null and self::$instance = new self();
        return self::$instance;
    }

    public function getList($record_id){
        $where['record_id']=$record_id;
        $where['isshow']=1;
        $res = $this->where($where)->select();
        $res || [];
        return $res;
    }

    /**
     * 新增或更新项目
     * @return boolean fasle 失败 ， int  成功 返回完整的数据
     * @author huajie <banhuajie@163.com>
     */
    public function update(){

        /* 获取数据对象 */
        $data = $this->create($_POST);
        if(empty($data)){
            return false;
        }


        /* 添加或新增项目 */
        if(empty($data['id'])){ //新增数据

            $data['create_at']=date('Y-m-d H:i:s');
            $id = $this->data($data)->add(); //添加评论
            RecordModel::singleton()->incCommentCount($data['record_id']);
            if(!$id){
                $this->error = '新增评论出错！';
                return false;
            }
        } else { //更新数据

            $status = $this->save($data); //更新基础内容
            if(false === $status){
                $this->error = '更新评论出错！';
                return false;
            }
        }

        //内容添加或更新完成
        return $data;

    }

    //删除经纪人
    public function del($map,$record_id){
        RecordModel::singleton()->descCommentCount($record_id);
        return $this->where($map)->save(['isshow'=>0]);

    }
}